/**
 * 
 */
package cz.cvut.fit.bugtrac.manager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import cz.cvut.fit.bugtrac.dao.AuthorDAO;
import cz.cvut.fit.bugtrac.dao.GraphDAO;
import cz.cvut.fit.bugtrac.exception.BugTracException;
import cz.cvut.fit.bugtrac.publicmodel.dto.graph.GraphDTO;
import cz.cvut.fit.bugtrac.publicmodel.dto.metadata.AuthorDTO;
import cz.cvut.fit.bugtrac.publicmodel.dto.metadata.AuthorsDTO;
import cz.cvut.fit.bugtrac.publicmodel.dto.metadata.MetadataDTO;
import cz.cvut.fit.bugtrac.util.DozerUtil;

/**
 * @author Jarec
 * 
 */
@Component
public class MetadataManagerImpl implements MetadataManager {

	private static transient final Logger log = Logger
			.getLogger(MetadataManagerImpl.class.getName());

	private final MetadataDTO metadata;

	// properties injected by spring
	private DozerUtil dozerUtil;
	private AuthorDAO authorDAO;
	private GraphDAO graphDAO;

	public MetadataManagerImpl() {
		metadata = new MetadataDTO();
	}

	@Override
	@PostConstruct
	public void reinitialize() throws BugTracException {
		log.fine("Reinitializing metadata...");
		Collection<AuthorDTO> author;
		try {
			author = dozerUtil.mapList(authorDAO.getAuthors(), AuthorDTO.class);
			log.info("Authors in DB: " + author.size());
		} catch (Exception e) {
			e.printStackTrace();
			log.warning("Cannot get Authors from DB: " + e.getMessage());
			// return empty list in case of exception
			author = new ArrayList<AuthorDTO>();
		}
		AuthorsDTO authors = new AuthorsDTO();
		authors.setAuthor(author);

		metadata.setAuthors(authors);
		try {
			GraphDTO snapshot = graphDAO.getLastSnapshots();

			metadata.setLastUpdate(snapshot.getCreated());
		} catch (Exception e) {
			log.info("Cannot get last snapshot - " + e.getMessage());
			metadata.setLastUpdate(Calendar.getInstance().getTime());
		}

	}

	@Override
	public MetadataDTO getMetadata() {
		return metadata;
	}

	@Override
	@Value("${project.name}")
	public void setName(String name) {
		metadata.setName(name);

	}

	@Override
	@Value("${project.description}")
	public void setDescription(String description) {
		metadata.setDescription(description);

	}

	@Override
	@Value("${update_interval}")
	public void setUpdateInterval(Long updateInterval) {
		metadata.setUpdateInterval(updateInterval);

	}

	/**
	 * @param dozerUtil
	 *            the dozerUtil to set
	 */
	@Autowired
	public void setDozerUtil(DozerUtil dozerUtil) {
		this.dozerUtil = dozerUtil;
	}

	/**
	 * @param authorDAO
	 *            the authorDAO to set
	 */
	@Autowired
	public void setAuthorDAO(AuthorDAO authorDAO) {
		this.authorDAO = authorDAO;
	}

	/**
	 * 
	 * @return
	 */
	public GraphDAO getGraphDAO() {
		return graphDAO;
	}

	/**
	 * 
	 * @param graphDAO
	 */
	@Autowired
	public void setGraphDAO(GraphDAO graphDAO) {
		this.graphDAO = graphDAO;
	}

}